#include <QCoreApplication>
#include <event2/event.h>
#include <event2/listener.h>
#include <iostream>
#include <signal.h>
#include <QDebug>

void Ctrl_c(int sock,short what,void* arg){
    qDebug() << "ctrl_c";
//    event* ev = (event*)arg;
//    //判断ev的当前状态是否为非未决
//    if(!evsignal_pending(ev,NULL)){
//        event_del(ev);
//        event_add(ev,NULL);
//    }
}

void Kill(int sock,short what,void* arg){
    qDebug() << "kill";
}


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if(signal(SIGPIPE,SIG_IGN) == SIG_ERR){
        return -1;
    }

    event_base* base = event_base_new();

    if(!base){
        return -1;
    }

    //event* csig = evsignal_new(base,SIGINT,Ctrl_c,base);
    //添加ctrl_c信号事件,event_self_cbarg传递事件对象本身
    event* csig = event_new(base,SIGINT,EV_SIGNAL | EV_PERSIST,Ctrl_c,event_self_cbarg());
    if(csig == NULL){
        qDebug() << "csig error";
        return -2;
    }
    event* ksig = event_new(base,SIGTERM,EV_SIGNAL | EV_PERSIST,Kill,event_self_cbarg());
    timeval val = {2,0};
    if(event_add(csig,&val) != 0){
        qDebug() << "add fail";
        return -2;
    }
    //event_add(ksig,0);
    //event_base_dispatch(base);
    event_base_loop(base,EVLOOP_NO_EXIT_ON_EMPTY);//事件循环中为空也不退出
    return 0;
}
